﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DeviceFinder.Models;

namespace DeviceFinder.DAL
{
    public class DeviceDAL : BaseDAL
    {
        public IQueryable<DEVICE> GetAllDevices(string userEmail)
        {
            var lstDevices = from device in entities.DEVICEs
                             where device.EMAIL_USER == userEmail
                             select device;

            return lstDevices;
        }

        public DEVICE GetDeviceByID(int IDDevice)
        {
            var lstdevice = (from device in entities.DEVICEs
                             where device.IDDEVICE == IDDevice
                          select device).ToList<DEVICE>();
            return lstdevice[0];
        }

        public IQueryable<DEVICE> GetAllDevicesInUse(string userEmail)
        {
            var lstDevices = from device in entities.DEVICEs
                             where device.EMAIL_USER == userEmail && device.IS_UNINSTALL == false
                             select device;

            return lstDevices;
        }

        public bool DeleteDevice(int idDevice)
        {
            var lstDevices = from dev in entities.DEVICEs
                             where dev.IDDEVICE == idDevice
                             select dev;

            if(lstDevices!= null && lstDevices.Count() > 0)
            {
                try
                {
                    TrackLogDAL tracklogDal = new TrackLogDAL();
                    if (tracklogDal.Delete(lstDevices.First().IDDEVICE))
                    {
                        entities.DEVICEs.Remove(lstDevices.First());
                        entities.SaveChanges();
                        return true;
                    }
                }
                catch (System.Exception ex)
                {
                    return false;
                }
            }

            return false;
        }

        public bool ChangeDeviceInfo(int id, string name, bool isFollow, bool isCommand, string message, int webcamCapture, string homepage)
        {
            var lstDevice = from d in entities.DEVICEs
                            where d.IDDEVICE == id && d.IS_UNINSTALL == false
                            select d;

            if (lstDevice != null && lstDevice.Count() > 0)
            {
                DEVICE first = lstDevice.First();
                first.NAME = name;
                first.IS_FOLLOW = isFollow;
                first.IS_COMMAND_ACTIVE = isCommand;
                first.MESSAGE = message;
                first.HOMEPAGE = homepage;
                first.WEBCAM_CAPTURE = webcamCapture;
                try
                {
                    entities.SaveChanges();
                    return true;
                }
                catch (System.Exception ex)
                {
                	
                }
            }

            return false;
        }

        public CreateDeviceStatus CreateDevice(out int id, string userEmail, string deviceName, string typeOfDevice, bool isFollow, string homepage, int webcamCapture, string message, bool isCommandActive)
        {
            id = -1;
            CreateDeviceStatus ret = CreateDeviceStatus.Success;
            // Check input

            if (ret != CreateDeviceStatus.Success)
                return ret;

            // Create Device Object
            lock (entities)
            {

                DEVICE device = new DEVICE();

                device.EMAIL_USER = userEmail;
                device.NAME = deviceName;
                device.TYPE = typeOfDevice; // why type is string from user input? how to check ?
                device.IS_FOLLOW = isFollow;
                device.HOMEPAGE = homepage;
                device.IS_UNINSTALL = false;
                device.WEBCAM_CAPTURE = webcamCapture; // why int?
                device.MESSAGE = message;
                device.IS_COMMAND_ACTIVE = isCommandActive;
                entities.DEVICEs.Add(device);
                entities.SaveChanges();


                //var last = from p in entities.DEVICEs
                //           group p by p.IDDEVICE into grp
                //           let MaxOrderDatePerPerson = grp.Max(g => g.IDDEVICE)
                //           from p in grp
                //           where p.IDDEVICE == MaxOrderDatePerPerson
                //           select p;

                List<DEVICE> getID = (from devices in entities.DEVICEs
                                      orderby devices.IDDEVICE descending
                                      select devices).ToList<DEVICE>();


                if (getID != null && getID.Count() > 0)
                    id = getID[0].IDDEVICE;
                
                return ret;
            }
        }
    }

    [Flags]
    public enum CreateDeviceStatus
    {
        Success = 0,
        DuplicateDevice = 1,        
    }
}